package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Job;
import com.dyb.recuitment.entity.Resume;
import com.dyb.recuitment.entity.UserLog;
import com.dyb.recuitment.service.IJobService;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 职位表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-18
 */
@RestController
@Api(tags = "职位管理(Job)")
@RequestMapping("/job")
public class JobController {

    @Autowired
    private IJobService jobService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "添加职位", notes = "通过标题、内容、工作地址、学历要求、经验要求、就职类型、职位类型、" +
            "工资、公司、职位所属、创建人添加职位 " +
            "{\"title\":\"\",\"content\":\"\",\"addr\":\"\",\"education\":\"\",\"experience\":\"\",\"type\":\"\",\"category\":\"\"" +
            ",\"money\":\"\",\"company\":\"\",\"tag\":\"\",\"createBy\":1}")
    @ApiImplicitParam(name = "job", value = "Job类")
    @PostMapping("/add")
    public Object add(@RequestBody Job job) {
        Object data = null;
        try {
            data = jobService.add(job);
            userLogService.add(new UserLog(job.getCreateBy(), "添加职位"));
            if (data == null || (int) data == 0) {
                return Result.failure(data, "添加失败!");
            }
            return Result.success(data, "添加成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "职位详情", notes = "通过ID查询职位详情 {\"id\":1}")
    @ApiImplicitParam(name = "job", value = "Job类")
    @PostMapping("/selectById")
    public Object selectById(@RequestBody Job job) {
        Object data = null;
        try {
            data = jobService.getById(job);
            if (data == null) {
                return Result.warning(data, "职位为空!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改职位", notes = "修改职位 {\"\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "job", value = "Job类")
    @PostMapping("/update")
    public Object update(@RequestBody Job job) {
        Object data = null;
        try {
            data = jobService.update(job);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "删除职位", notes = "通过ID删除职位" +
            "{\"id\":1}")
    @ApiImplicitParam(name = "job", value = "Job类")
    @PostMapping("/delete")
    public Object delete(@RequestBody Job job) {
        Object data = null;
        try {
            data = jobService.delete(job);
//            userLogService.add(new UserLog(resume.getUserId(), "删除职位"));
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "删除失败!");
            }
            return Result.success(data, "删除成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "按条件查询职位", notes = "按条件查询职位 {\"addr\":\"\"}")
    @ApiImplicitParam(name = "job", value = "Job类")
    @PostMapping("/selectByJob")
    public Object selectByJob(@RequestBody Job job) {
        Object data = null;
        try {
            data = jobService.selectByJob(job);
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无职位!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询所有职位", notes = "查询所有职位")
    @GetMapping("/selectAllJob")
    public Object selectAllJob() {
        Object data = null;
        try {
            data = jobService.selectAllJob();
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无职位!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
